$(function () {
    var uiContent = $('#uiContent');
    var prizeList = $('#prizeList');
    var loadingDiv = $('#loadingDiv');

    var openId = $.getUrlParam('openId');
    if (openId) {
        $.wxJsApiConfig(function () {
            main(openId);
        });
    } else {
        $.gotoErrorPage(uiContent);
    }

    // 主函数
    function main(openId) {
        uiContent.show();
        loadData(openId, 0);
    }

    // 生成列表中的一行
    function generateNode(userPrize) {
        var node = '<div class="weui_media_box weui_media_text">' +
            '<h3 class="weui_media_title">{title}</h3>' +
            '<div>' +
            '<p class="weui_media_desc" style="font-size: 14px; color: #666; margin-bottom: 10px;">{prizeResult}</p>' +
            '<p class="weui_media_desc" style="font-size: 14px; color: #666;">{desc}</p>' +
            '<p class="weui_media_desc" style="margin-top: 15px; color: #cecece;">领取时间：{lastUpdateTime}</p>' +
            '<p class="weui_media_desc" style="color: #cecece;">有效期至：{endTime}</p>' +
            '</div>' +
            '<ul class="weui_media_info" style="float: right; padding-bottom: 0;">' +
            '<li class="weui_media_info_meta">' +
            '<a href="{newsUrl}" class="weui_btn weui_btn_mini weui_btn_default" style="font-size: 12px;">阅读图文</a>' +
            '</li>' +
                //'<li class="weui_media_info_meta weui_media_info_meta_extra">' +
                //'<a href="{usageUrl}" class="weui_btn weui_btn_mini weui_btn_default" style="font-size: 12px;">使用说明</a>' +
                //'</li>' +
            '</ul>' +
            '<div style="clear: both;"></div>' +
            '</div>';

        var titleNode = '{telephone}<span style="padding: 2px 5px; margin: 0 0 0 8px; font-weight: bold;">{prizeName}</span>';

        var title = titleNode.replace('{telephone}', userPrize.telephone).replace('{prizeName}', userPrize.pushNews.prizeNumber + userPrize.pushNews.prize.name);
        var expiredTime = '永远';
        if (userPrize.pushNews.prize.expiredDay) {
            expiredTime = new Date((userPrize.createTime + userPrize.pushNews.prize.expiredDay * 24 * 3600) * 1000).format('yyyy-MM-dd hh:mm')
        }
        var prizeResult = userPrize.prizeResult;
        if ('ok' == prizeResult) {
            prizeResult = '成功';
        }
        return node.replace('{title}', title)
            .replace('{prizeResult}', '领奖结果：' + prizeResult)
            .replace('{desc}', userPrize.pushNews.prize.description)
            .replace('{lastUpdateTime}', new Date(userPrize.lastUpdateTime * 1000).format('yyyy-MM-dd hh:mm'))
            .replace('{endTime}', expiredTime)
            .replace('{newsUrl}', userPrize.pushNews.newsUrl)
            .replace('{usageUrl}', userPrize.pushNews.prize.usageUrl);
    }

    // 生成空内容页面
    function generateEmptyNode(url) {
        return '<div class="weui_msg">' +
            '<div class="weui_icon_area"><i class="weui_icon_safe_warn weui_icon_msg"></i></div>' +
            '<div class="weui_text_area"><h2 class="weui_msg_title">您还没有礼品</h2></div>' +
            '<div class="weui_opr_area"><p class="weui_btn_area"><a href="' + url + '" class="weui_btn weui_btn_primary">立即领礼品</a></p></div>' +
            '<div class="weui_extra_area"></div>';
    }

    // 加载数据
    function loadData(openId, pageNum) {
        loadingDiv.show();
        $.restClient('/mobile/prize/get/' + openId + '/' + pageNum, null, function (data) {
            loadingDiv.hide();
            if (data && data.data) {
                if (data.data.content && data.data.content.length) {
                    if (pageNum == 0) {
                        prizeList.empty();
                    }
                    $.each(data.data.content, function (index, oneData) {
                        prizeList.append(generateNode(oneData));
                    });
                } else {
                    prizeList.append(generateEmptyNode($.howGetPrize))
                }

                if (data.data.last) {
                    $(document.body).destroyInfinite();
                } else {
                    $(document.body).infinite().on("infinite", function () {
                        loadData(openId, pageNum + 1);
                    });
                }
            }
        });
    }

    window.gotoUnFetchPrizePage = function () {
        window.location.href = '/mobile/showUnFetchPrize.html?time=' + (+new Date()) + '&openId=' + $.getUrlParam('openId');
    };
});
